import wifiManager from '@ohos.wifiManager';
import { log } from '../../util/index';

import type { WifiDeviceConfig, WifiLinkedInfo, WifiCommonHandler } from './type'

const TAG = 'connect_wifi_system'

export function isWifiActive(): boolean {
  try {
    const result = wifiManager.isWifiActive();
    log.info(TAG, 'isWifiActive success', result)
    return result
  } catch (err) {
    log.error(TAG, 'wifiManager fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function enableWifi(): boolean {
  try {
    wifiManager.enableWifi();
    log.info(TAG, 'enableWifi success')
    return true
  } catch (err) {
    log.error(TAG, 'enableWifi fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function disableWifi(): boolean {
  try {
    wifiManager.disableWifi();
    log.info(TAG, 'disableWifi success')
    return true
  } catch (err) {
    log.error(TAG, 'disableWifi fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function isConnected(): boolean {
  try {
    const result = wifiManager.isConnected();
    log.info(TAG, 'isConnected success', result)
    return result
  } catch (err) {
    log.error(TAG, 'isConnected fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function connectToDevice(config: WifiDeviceConfig): boolean {
  try {
    wifiManager.connectToDevice(config);
    log.info(TAG, 'connectToDevice success')
    return true
  } catch (err) {
    log.error(TAG, 'connectToDevice fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function disconnect(): boolean {
  try {
    wifiManager.disconnect();
    log.info(TAG, 'disconnect success')
    return true
  } catch (err) {
    log.error(TAG, 'disconnect fail', JSON.stringify(err) ?? '')
    return false
  }
}

export async function getLinkedInfo(): Promise<WifiLinkedInfo> {
  try {
    const linkedInfo = await wifiManager.getLinkedInfo();
    log.info(TAG, 'getLinkedInfo success', JSON.stringify(linkedInfo))
    return linkedInfo
  } catch (err) {
    log.error(TAG, 'getLinkedInfo fail', JSON.stringify(err) ?? '')
    return null
  }
}

export function getDeviceConfigs(): WifiDeviceConfig[] {
  try {
    const deviceConfigs = wifiManager.getDeviceConfigs();
    log.info(TAG, 'getDeviceConfigs success', JSON.stringify(deviceConfigs.length))
    return deviceConfigs
  } catch (err) {
    log.error(TAG, 'getDeviceConfigs fail', JSON.stringify(err) ?? '')
    return []
  }
}

export function removeDevice(id: number): boolean {
  try {
    wifiManager.removeDevice(id);
    log.info(TAG, 'removeDevice success', JSON.stringify(id))
    return true
  } catch (err) {
    log.error(TAG, 'removeDevice fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function onWifiStateChange(handler: WifiCommonHandler): boolean {
  try {
    wifiManager.on('wifiStateChange', handler);
    log.info(TAG, 'onWifiStateChange success')
    return true
  } catch (err) {
    log.error(TAG, 'onWifiStateChange fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function offWifiStateChange(handler?: WifiCommonHandler): boolean {
  try {
    wifiManager.off('wifiStateChange', handler);
    log.info(TAG, 'offWifiStateChange success')
    return true
  } catch (err) {
    log.error(TAG, 'offWifiStateChange fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function onWifiConnectionChange(handler: WifiCommonHandler): boolean {
  try {
    wifiManager.on('wifiConnectionChange', handler);
    log.info(TAG, 'onWifiConnectionChange success')
    return true
  } catch (err) {
    log.error(TAG, 'onWifiConnectionChange fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function offWifiConnectionChange(handler?: WifiCommonHandler): boolean {
  try {
    wifiManager.off('wifiConnectionChange', handler);
    log.info(TAG, 'offWifiConnectionChange success')
    return true
  } catch (err) {
    log.error(TAG, 'offWifiConnectionChange fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function onWifiRssiChange(handler: WifiCommonHandler): boolean {
  try {
    wifiManager.on('wifiRssiChange', handler);
    log.info(TAG, 'onWifiRssiChange success')
    return true
  } catch (err) {
    log.error(TAG, 'onWifiRssiChange fail', JSON.stringify(err) ?? '')
    return false
  }
}

export function offWifiRssiChange(handler?: WifiCommonHandler): boolean {
  try {
    wifiManager.off('wifiRssiChange', handler);
    log.info(TAG, 'offWifiRssiChange success')
    return true
  } catch (err) {
    log.error(TAG, 'offWifiRssiChange fail', JSON.stringify(err) ?? '')
    return false
  }
}
